package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Module;
import com.sl.au.entity.ScadaBasePoint;
import com.sl.au.entity.ScadaRtuBaseInfo;
import com.sl.au.entity.User;
import com.sl.au.service.ExdeScadaRtuBaseService;
import com.sl.au.service.OptionService;

@RestController
@RequestMapping(value = "/ui/scadartubase/")
public class ExdeScadaRtuBaseController extends BaseController {
	
	@Autowired
	ExdeScadaRtuBaseService exdeScadaRtuBaseService;
	
	@Autowired
	OptionService optionService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		Module module = this.getPageModule("scada");
		ModelAndView modelAndView = this.createLayoutView("scada/scadabasepointindex");
		modelAndView.addObject("danweis", optionService.getRepository().findByGroup("单位"));
		modelAndView.addObject("module", module);
		
		return modelAndView;
	}
	
	/**
	 * 获取页面数据
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		Account a = this.getMyAccount();
		Module module = this.getPageModule("scadabasepointindex");
		String dataFilter = module.getDataFilter();
		User user = a.getUser();
		//this.getQueryParams(qMap,numMap);
		
		String userId = a.getUser().getId();
		String Superior = a.getUser().getSaleArea().getSuperior();//权限部门
		String areaName = a.getUser().getSaleArea().getAreaName();
		if (key == null) {
			key = "";
		}
		if (userId == null) {
			userId = "";
		}
		if (Superior == null) {
			Superior = "";
		}
		if (areaName == null) {
			areaName = "";
		}
		if (dataFilter == null) {
			dataFilter = "";
		}
		condition.put("key", key);
		condition.put("userId", userId);
		condition.put("superior", Superior);
		condition.put("areaName", areaName);
		condition.put("dataFilter", dataFilter);
		List<ScadaRtuBaseInfo> scadaRtuBaseInfos = exdeScadaRtuBaseService.getScadaRtuBaseInfos(start, length, condition, user, "createTime", "desc");
		long count = exdeScadaRtuBaseService.getScadaRtuBaseInfosCount(0, Integer.MAX_VALUE, condition, user, "createTime", "desc");
		map.put("data", scadaRtuBaseInfos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 获取子表
	 */
	@RequestMapping(value = "getpageScadaBasePoint", method = RequestMethod.GET)
	public Map<String, Object> getPageScadaBasePoint(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String ScadaRtuBaseInfoId = request.getParameter("ScadaRtuBaseInfoId");
		String key = request.getParameter("key");
		if(key == null){
			key = "";
		}
		condition.put("ScadaRtuBaseInfoId", ScadaRtuBaseInfoId);
		condition.put("key", key);
		List<ScadaBasePoint> scadaRtuBaseInfos = exdeScadaRtuBaseService.getScadaBasePoints(start, length, condition, "createTime", "desc");
		long count = exdeScadaRtuBaseService.getScadaBasePointsCount(0, Integer.MAX_VALUE, condition, "createTime", "desc");
		map.put("data", scadaRtuBaseInfos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 保存主表
	 * @param scadaRtuBaseInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveScadaRtuBaseInfo", method = RequestMethod.POST)
	public Map<String, Object> saveScadaRtuBaseInfo(@RequestBody ScadaRtuBaseInfo scadaRtuBaseInfo, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		if(scadaRtuBaseInfo.getId()==null||scadaRtuBaseInfo.getId().equals("")){
			scadaRtuBaseInfo.setId(java.util.UUID.randomUUID().toString());
		}
		User user = a.getUser();
		scadaRtuBaseInfo.setUser(user);
		exdeScadaRtuBaseService.getExdeScadaRtuBaseRepository().save(scadaRtuBaseInfo);
		return map;
	}
	/**
	 * 保存子表
	 * @param scadaRtuBaseInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveScadaBasePoint", method = RequestMethod.POST)
	public Map<String, Object> saveScadaBasePoint(@RequestBody ScadaBasePoint scadaBasePoint, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		if(scadaBasePoint.getId()==null||scadaBasePoint.getId().equals("")){
			scadaBasePoint.setId(java.util.UUID.randomUUID().toString());
		}
		User user = a.getUser();
		scadaBasePoint.setUser(user);
		if(scadaBasePoint.getSwitchingValue()==null||scadaBasePoint.getSwitchingValue().equals("")){
			scadaBasePoint.setSwitchingValue(null);
		}
		exdeScadaRtuBaseService.getScadaBasePointRepository().save(scadaBasePoint);
		return map;
	}
	//删除点位
	@RequestMapping(value = "ScadaBasePoint", method = RequestMethod.GET)
	public Map<String, Object> ScadaBasePoint(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		if (StringUtils.hasText(ids)) {
			Account a = this.getMyAccount();
			User user = a.getUser();
			exdeScadaRtuBaseService.deleteScadaBasePoint(getStringIds(ids));
		}
		map.put("code", 0);
		return map;
	}
	//删除单元
	@RequestMapping(value = "ScadaRtuBaseInfo", method = RequestMethod.GET)
	public Map<String, Object> ScadaRtuBaseInfo(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		if (StringUtils.hasText(ids)) {
			Account a = this.getMyAccount();
			User user = a.getUser();
			exdeScadaRtuBaseService.deleteScadaRtuBaseInfo(getStringIds(ids));
		}
		map.put("code", 0);
		return map;
	}
	
	public String getStringIds(String ids) {
		String[] id = ids.split(",");
		ids = "";
		for(int i=0;i<id.length;i++){
			ids += "'" + id[i] + "',";
		}
		ids = ids.substring(0, ids.length()-1);
		return ids;
	}

}
